package com.ikun.hospayservice.litener;

import com.ikun.hospayservice.config.TimeoutOrderMQConfig;
import com.ikun.hospayservice.service.WxPayService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


/**
 * 超时消息监听器
 */
@Slf4j
@Component
public class TimeoutOrderListener {


    @Resource
    private WxPayService wxPayService;

    /**
     * 处理超时消息
     *
     * @param tradeNo
     */
    @RabbitListener(queues = {TimeoutOrderMQConfig.DEADLETTER_ORDER_QUEUE})
    public void handleTimeoutOrder(String tradeNo, Channel channel, Message message) {
        log.info("订单发生超时{}", tradeNo);
        try {
            wxPayService.cancelOrder(tradeNo);
            //手动确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception ex) {
            log.error("超时订单处理发生异常", ex);
        }
    }
}
